<template>
  <div>
    <!-- <div>
      <button @click="printEditorHtml">print html</button>
      <button @click="insertTextHandler">insert text</button>
      <button @click="disableHandler">disable</button>
    </div> -->
    <div style="border: 1px solid #ccc; margin-top: 10px;">
      <!-- 工具栏 -->
      <Toolbar style="border-bottom: 1px solid #ccc" :editor="editor" :defaultConfig="toolbarConfig" />
      <!-- 编辑器 -->
      <Editor style="height: 400px; overflow-y: hidden;" :defaultConfig="editorConfig" v-model="html" @onChange="onChange" @onCreated="onCreated" />
    </div>
    <!-- <div style="margin-top: 10px;">
      <textarea v-model="html" readonly style="width: 100%; height: 200px; outline: none;"></textarea>
    </div> -->
  </div>
</template>

<script>
// uid
const uidGenerator = () => {
  return '-' + parseInt(Math.random() * 10000 + 1, 10);
};
// 文件名
const getFileName = (path) => {
  if (path.lastIndexOf('\\') >= 0) {
    let reg = new RegExp('\\\\', 'g');
    path = path.replace(reg, '/');
  }
  return path.substring(path.lastIndexOf('/') + 1);
};
import { Editor, Toolbar } from '@wangeditor/editor-for-vue';
import { upload } from '@/utils/upload';
import { mapGetters } from 'vuex';
export default {
  name: 'MyEditor',
  components: { Editor, Toolbar },
  data() {
    return {
      editor: null,
      html: '',
      toolbarConfig: {
        // toolbarKeys: [ /* 显示哪些菜单，如何排序、分组 */ ],
        // excludeKeys: [ /* 隐藏哪些菜单 */ ],
        excludeKeys: ['insertImage', 'insertVideo', 'group-video'], //去掉网络上传图片和视频
      },
      editorConfig: {
        placeholder: '请输入内容...',
        // autoFocus: false,
        // 所有的菜单配置，都要在 MENU_CONF 属性下

        MENU_CONF: {
          uploadImage: {
            customUpload: this.uploadImageMe, // this.uploadImageMe 是 methods 中的一个普通方法
          },
        },
      },
    };
  },
  computed: {
    ...mapGetters(['imagesUploadApi']),
  },
  methods: {
    onCreated(editor) {
      this.editor = Object.seal(editor); // 【注意】一定要用 Object.seal() 否则会报错
    },
    onChange(editor) {
      this.$emit('printEdito', editor.getHtml());
      console.log('onChange', editor.getHtml()); // onChange 时获取编辑器最新内容
    },
    uploadImageMe(file, insertFn) {
      // file 即选中的文件
      // 自己实现上传，并得到图片 url alt href
      //上传方法
      upload(this.imagesUploadApi, file).then((res) => {
        console.log(res);
        // 最后插入图片
        insertFn(res.data.result, res.data.result, res.data.result);
      });
    },
    insertTextHandler() {
      const editor = this.editor;
      if (editor == null) return;
      editor.insertText(' hello ');
    },
    printEditorHtml() {
      const editor = this.editor;
      if (editor == null) return;
      this.$emit('printEdito', editor.getHtml());
      // console.log(editor.getHtml());
    },
    disableHandler() {
      const editor = this.editor;
      if (editor == null) return;
      editor.disable();
    },
    childFunction(content) {
      this.html = content ? content : '';
    },
  },
  mounted() {
    // 模拟 ajax 请求，异步渲染编辑器
    // setTimeout(() => {
    //   this.html = '<p>Ajax 异步设置内容 HTML</p>';
    // }, 1500);
  },
  beforeDestroy() {
    const editor = this.editor;
    if (editor == null) return;
    editor.destroy(); // 组件销毁时，及时销毁 editor ，重要！！！
  },
};
</script>

<style src="@wangeditor/editor/dist/css/style.css"></style>